// 数组操作

//创建数组方式
{
  //总结：注意长度和具体值的区分
  //当Array和new Array传值是一个数字时，就是创建长度，否则就视为数组的值
  console.log("创建数组方式");
  console.log(Array(5)); //[ <5 empty items> ]
  console.log(new Array(5)); //[ <5 empty items> ]
  console.log(Array.of(5)); //[5]

  console.log(Array(5, 3)); //[5,3]
  console.log(new Array(5, 3)); //[5,3]
  console.log(Array.of(5, 3)); //[5,3]
}

{
  console.log("Array.from");
  console.log(Array.from({ name: "1", age: "2" })); //[]
  console.log(Array.from({ 0: 1, 1: 2 })); //[]
}

{
  console.log("对象转数组");
  // 对象转数组时，对象中必须含有length属性，这个对象才类似数组对象
  //错误写法，因为没有length属性
  console.log(Array.from({ name: "1", age: "2" })); //[]
  console.log(Array.from({ 0: 1, 1: 2 })); //[]

  //错误写法，因为键没有使用索引
  console.log(Array.from({ name: "1", age: "2", length: 4 })); //[ undefined, undefined, undefined, undefined ]

  //正确写法：必须包含length属性，而且键必须是所引，才能对应的赋值
  console.log(Array.from({ 0: 1, 1: 2, 4: 4, length: 5 })); //[ 1, 2, undefined, undefined, 4 ]

  console.log(Array.prototype.entries.call({ name: "1", age: "2" })); //返回迭代器对象
  console.log(Array.from(Array.prototype.entries.call({ 0: 1, 1: 2 })));

  //从nodeList创建数组
  // const images = document.querySelectorAll("img");
  // const sources = Array.from(images, (image) => image.src);
}

{
  // 类数组转数组
  // 扩展运算符
  function checkArgs() {
    return [...arguments];
  }

  let result = checkArgs(1, 2);
  console.log(result); // [1, 2]

  // Array.from()
  function checkArgs() {
    return Array.from(arguments);
  }

  let result1 = checkArgs(1, 2);
  console.log(result1); // [1, 2]

  // slice()
  function checkArgs() {
    return Array.prototype.slice.call(arguments);
  }

  let result2 = checkArgs(1, 2);
  console.log(result2); // [1, 2]
}

// 插入元素
{
  let a = [1, 2, 3];

  a.splice(1, 0, 3);
  console.log(a);
}

// 删除数组元素
